<?php

// load the shared functions into the global scope
// these happen to be the functions related to
require_once('spec_functions.php');

// load the base class
require_once('jsonrpc2.class.php');

// create a instance of the transport layer
$server = new jsonrpc2(
	array(
		'subtract' 		=> 'subtract',
		'sum' 			=> 'sum',
		'notify_hello' 	=> 'notify_hello',
		'notify_sum' 	=> 'notify_sum',
		'get_data' 		=> 'get_data'
	)
);

$output = array();
$output[] = 'Examples from the JSON-RPC 2 Specification';

$examples = array();

$examples[] = array(
	'description' => 'rpc call with positional parameters #1',
	'request' => '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}',
	'expected_response' => '{"jsonrpc": "2.0", "result": 19, "id": 1}'
);
$examples[] = array(
	'description' => 'rpc call with positional parameters #2',
	'request' => '{"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 1}',
	'expected_response' => '{"jsonrpc": "2.0", "result": -19, "id": 1}'
);

$examples[] = array(
	'description' => 'rpc call with named parameters #1',
	'request' => '{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}',
	'expected_response' => '{"jsonrpc": "2.0", "result": 19, "id": 3}'
);

$examples[] = array(
	'description' => 'rpc call with named parameters #2',
	'request' => '{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}',
	'expected_response' => '{"jsonrpc": "2.0", "result": 19, "id": 4}'
);

$examples[] = array(
	'description' => 'notification #1',
	'request' => '{"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}',
	'expected_response' => 'null'
);

$examples[] = array(
	'description' => 'notification #2',
	'request' => '{"jsonrpc": "2.0", "method": "foobar"}',
	'expected_response' => 'null'
);

$examples[] = array(
	'description' => 'rpc call of non-existent method',
	'request' => '{"jsonrpc": "2.0", "method": "foobar", "id": "1"}',
	'expected_response' => '{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": "1"}'
);


$examples[] = array(
	'description' => 'rpc call with invalid JSON',
	'request' => '{"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]',
	'expected_response' => '{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error."}, "id": null}'
);

$examples[] = array(
	'description' => 'rpc call with invalid JSON',
	'request' => '{"jsonrpc": "2.0", "method": 1, "params": "bar"}',
	'expected_response' => '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}'
);


$examples[] = array(
	'description' => 'rpc call Batch, invalid JSON',
	'request' => '[ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},{"jsonrpc": "2.0", "method" ]',
	'expected_response' => '{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error."}, "id": null}'
);

$examples[] = array(
	'description' => 'rpc call with an empty Array',
	'request' => '[]',
	'expected_response' => '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}'
);

$examples[] = array(
	'description' => 'rpc call with an invalid Batch (but not empty)',
	'request' => '[1]',
	'expected_response' => '[{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}]'
);

$examples[] = array(
	'description' => 'rpc call with invalid Batch',
	'request' => '[1,2,3]',
	'expected_response' => '[
        {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null},
        {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null},
        {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}
    ]'
);


$examples[] = array(
	'description' => 'rpc call Batch',
	'request' => '[
        {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
        {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
        {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
        {"foo": "boo"},
        {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
        {"jsonrpc": "2.0", "method": "get_data", "id": "9"} 
    ]',
	'expected_response' => '[
        {"jsonrpc": "2.0", "result": 7, "id": "1"},
        {"jsonrpc": "2.0", "result": 19, "id": "2"},
        {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null},
        {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found."}, "id": "5"},
        {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
    ]'
);

$examples[] = array(
	'description' => 'rpc call Batch',
	'request' => '[
        {"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]},
        {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}
    ]',
	'expected_response' => 'null'
);

foreach( $examples as &$example){	
	$example['response'] = json_encode( $server->dispatch( $example['request'] ) );	
	if( is_null($example['response'])){
		$example['response'] = '';
	}
}

foreach( $examples as &$example){
	$output[] = '';
	$output[] = $example['description'];
	$output[] = '-------------------------------';
	$output[] = 'request:'.$example['request'];
	// $diff = array_diff_assoc( json_decode( $example['response'], TRUE) , json_decode( $example['expected_response'], TRUE) );
	$output[] = 'expected:'. $example['expected_response'];
	$output[] = 'response:'.$example['response'];
	// $output[] = print_r( $diff, true );
}

echo implode("\n", $output);